package com.yuwei.movie

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import kotlin.math.max

    class FlowBoxLayout @JvmOverloads constructor(context: Context,attrs:AttributeSet,defStyleAttr:Int):ViewGroup(context,attrs,defStyleAttr) {

    private var mItemOnClickListener:ItemOnClickListener? = null
    fun setOnClickListener(l:ItemOnClickListener){
        mItemOnClickListener = l
    }

    /**
     * 设置文本集合添加
     */
    fun addKeyWords(list:List<String>){
        if(list.isNullOrEmpty()) return
        //创建文本组件
        var itemView: TextView
        var itemLayoutParams:MarginLayoutParams
        //for循环添加item组件
        list.forEach {keywords ->
            itemView = TextView(context).apply {
                //文本信息
                text = keywords
                setTextColor(Color.BLACK)
                textSize = 16F
                //设置布局参数
                itemLayoutParams = MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT).apply {
                    setMargins(10,5,10,5)
                }
                layoutParams = itemLayoutParams
                //设置内边距
                setPadding(10,5,10,5)
                //设置背景
                setBackgroundResource(R.drawable.shape_keyword_bg)
                //设置点击事件
                setOnClickListener { v ->
                    mItemOnClickListener?.onItemClick(v,keywords)
                }
            }
            //添加item组件到容器
            addView(itemView)
        }
        //重新布局
        requestLayout()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //测量子组件大小
        measureChildren(widthMeasureSpec,heightMeasureSpec)
        //重新设置容器组件大小
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),MeasureSpec.getSize(heightMeasureSpec))
    }
    override fun onLayout(p0: Boolean, p1: Int, p2: Int, p3: Int, p4: Int) {
        //获取子组件的数量
        val count = childCount
        var childView:View
        //坐标
        var cLeft = 0
        var cTop = 0
        var cRight = 0
        var cBottom = 0
        var cWidth = 0;var cHeight = 0
        var cLayoutParams:MarginLayoutParams
        var rowWidth = 0;var columnHeight = 0
        //容器的测量宽度
        val parentWith = measuredWidth

        //子组件左上角坐标
        var  top = 0;var left = 0
        for (index in 0 until count){
            childView = getChildAt(index)
            //获取子组件长和高（包含组件大小和外边距）
            cLayoutParams = childView.layoutParams as MarginLayoutParams
            cWidth = childView.measuredWidth +  cLayoutParams.leftMargin + cLayoutParams.rightMargin
            cHeight = childView.measuredHeight + cLayoutParams.topMargin + cLayoutParams.bottomMargin

            //判断是否需要换行
            if (rowWidth + cWidth > measuredWidth){
                columnHeight = cHeight
                rowWidth = cWidth
                top += cHeight
                left = 0
            }else{
                rowWidth += cWidth
                columnHeight = max(columnHeight,cHeight)//显示全子组件
            }
            //计算子组件上下左右的大小
            cLeft = left + cLayoutParams.leftMargin
            cTop = top + cLayoutParams.topMargin
            cRight = left + childView.measuredWidth
            cBottom = cTop + childView.measuredHeight

            //修改子组件的布局参数
            childView.layout(cLeft,cTop,cRight,cBottom)

            //更新left
            left += cWidth
        }
    }

    /**
     * 以下获取子组件的外边距
     */
    override fun generateDefaultLayoutParams(): LayoutParams {
        return MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
    }

    override fun generateLayoutParams(p: LayoutParams?): LayoutParams {
        return MarginLayoutParams(p)
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context,attrs)
    }
    /**
     * 定义Item的点击事件
     */
    interface  ItemOnClickListener{
        fun onItemClick(V:View,keywords:String)
    }
}